package org.soulraven.games.memory.ui.game;

import java.awt.GridLayout;

import javax.swing.JPanel;

import org.apache.log4j.Logger;

public class MainPanel extends JPanel {

	private static final Logger LOGGER = Logger.getLogger(MainPanel.class);

	private static final long serialVersionUID = 3035381312621586105L;

	private ButtonCard[][] cards;

	public MainPanel(ButtonCard[][] cards) {
		super();
		this.cards = cards;
		this.initialize();
	}

	private void initialize() {
		this.setLayout(new GridLayout(cards.length, cards[0].length, 5, 5));
		for (int i = 0; i < cards.length; i++) {
			for (int j = 0; j < cards[i].length; j++) {
				this.add(cards[i][j]);
			}
		}
	}

	public void disableAllCards() {
		ButtonCard card = null;
		for (int i = 0; i < cards.length; i++) {
			for (int j = 0; j < cards[i].length; j++) {
				card = cards[i][j];
				if (card.isSelectable()) {
					LOGGER.debug("Disabling Card number " + card.getNumber());
					card.setSelectable(Boolean.FALSE);
				}
			}
		}
	}

	public void enableAllSelectableCards() {
		ButtonCard card = null;
		for (int i = 0; i < cards.length; i++) {
			for (int j = 0; j < cards[i].length; j++) {
				card = cards[i][j];
				if (card.isHidden()) {
					LOGGER.debug("Enabling card number " + card.getNumber());
					card.setSelectable(Boolean.TRUE);
				}
			}
		}
	}

	public boolean areAllCardsDisabled() {
		ButtonCard card = null;
		for (int i = 0; i < cards.length; i++) {
			for (int j = 0; j < cards[i].length; j++) {
				card = cards[i][j];
				if (card.isEnabled()) {
					return false;
				}
			}
		}
		return true;
	}
}
